﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Mvc;
using Lilium.Web.Mvc.EntityFilters;

namespace Lilium.Web.Mvc.Html
{
    public static class AplhabetExtensions
    {
        public static IEnumerable<MvcHtmlString> GetAplhabetActionLinks(this HtmlHelper html, string allLinkText, string nonAlphaLinkText)
        {
            if (html == null) throw new ArgumentNullException("html");

            var aplhabet = GetAplhabet(html);
            
            yield return EntityFilterExtensions.FilterActionLink(html, allLinkText, aplhabet, Alphabet.Action_All);
            yield return EntityFilterExtensions.FilterActionLink(html, nonAlphaLinkText, aplhabet, Alphabet.Action_NoneAlpha);

            foreach (var letter in Enumerable.Range((int)aplhabet.FirstLetter, (int)aplhabet.LastLetter - (int)aplhabet.FirstLetter + 1))
            {
                var letterStr = ((char)letter).ToString();
                yield return EntityFilterExtensions.FilterActionLink(html, letterStr.ToUpper(), aplhabet, letterStr);
            }
        }

        public static IAlphabet GetAplhabet(this HtmlHelper html)
        {
            if (html == null) throw new ArgumentNullException("html");

            var modelMetadata = html.ViewData.ModelMetadata;
            if (modelMetadata == null)
                throw new InvalidOperationException("Model metadata required");

            var entityType = modelMetadata.ModelType.GetGenericArguments()[0];

            return Alphabet.GetAplhabet(html.ViewContext.Controller, entityType);
        }
    }
}
